package com.resou.reader.signin.style2;

import android.text.TextUtils;
import android.util.Log;

import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.data.ResouxsRepository;
import com.resou.reader.data.signin.model.SignIn;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.UserUtil;

public class SignIn2Presenter extends ResouBasePresenter<SignIn2Contract.View> implements SignIn2Contract.Presenter {
    private static final String TAG = "SignIn2Presenter";
    private final ResouxsRepository mRepository;
    //是否是第一次加载
    private boolean isFirstLoad = true;

    public SignIn2Presenter(SignIn2Contract.View view) {
        super(view);
        mRepository = ResouxsRepository.getInstance();
    }

    @Override
    public void loadSignInfo() {
        if (isFirstLoad) {
            mView.showProgress();
        }
        addCompositeDisposable(
                mRepository.loadSignInfo2(UserInstance.getToken())
                        .subscribeOn(getIOSchedulers())
                        .observeOn(getMainSchedulers())
                        .subscribe(result -> {
                                    if (result.getCode() == 0) {
                                        mView.showData(result.getData());
                                        mView.showContent();
                                        isFirstLoad = false;
                                    }
                                }, throwable -> mView.showError(throwable)
                        ));
    }

    public void signIn(int weekDay) {
        addCompositeDisposable(
                mRepository.signIn(UserUtil.getToken(), weekDay)
                        .subscribeOn(getIOSchedulers())
                        .observeOn(getMainSchedulers())
                        .subscribe(result -> {
                                    if (result.getCode() == 0) {
                                        SignIn data = result.getData();
                                        mView.onSignInSucceed(data);
                                        //签到成功需要刷新列表
                                        loadSignInfo();
                                        mView.showContent();
                                    }
                                }, throwable -> {
                                    mView.onSignInFailed(throwable);
                                    mView.showError(throwable);
                                }
                        ));
    }

    public void receiveWelfare(String taskId) {
        String token = UserInstance.getToken();
        if (TextUtils.isEmpty(token)) {
            ToastUtil.makeShortToast("您尚未登录，请先登录后重试");
            return;
        }
        addCompositeDisposable(
                mRepository.receiveWelfare(token, taskId)
                        .subscribeOn(getIOSchedulers())
                        .observeOn(getMainSchedulers())
                        .subscribe(result -> {
                            if (result.getCode() == 0) {
                                mView.setReceivingCompleted();
                            }
                        }, throwable -> mView.showError(throwable))
        );
    }
}
